﻿<!--
// (c) Copyright Microsoft Corporation.
// This source is subject to the Microsoft Public License (Ms-PL).
// Please see http://go.microsoft.com/fwlink/?LinkID=131993 for details.
// All other rights reserved.
-->

<UserControl
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:common="clr-namespace:System.Windows;assembly=System.Windows.Controls.Toolkit"
    xmlns:toolkit="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Toolkit"
    x:Class="System.Windows.Controls.Samples.MasterDetailSample">
    <StackPanel>

        <StackPanel.Resources>
            <common:HierarchicalDataTemplate x:Key="TaxonomyTemplate" ItemsSource="{Binding Subclasses}">
                <TextBlock Text="{Binding Classification}"/>
            </common:HierarchicalDataTemplate>
        </StackPanel.Resources>

        <ContentControl Content="Master/Detail" Style="{StaticResource Header}" />
        <TextBlock Style="{StaticResource Information}">
                Demonstrates using a TreeView in a Master/Detail scenario.
                <LineBreak/>
                <LineBreak/>
                Two different methods of displaying detail data are 
                demonstrated.  The Classification and Rank TextBlocks are 
                bound directly to the SelectedItem in the TreeView everytime 
                the selection changes.  The larger information section 
                simulates a lookup into another data source using the 
                currently selected item as a key.
        </TextBlock>
        
        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="200" />
                <ColumnDefinition Width="*" />
            </Grid.ColumnDefinitions>
        
            <toolkit:TreeView
                Grid.Column="0"
                x:Name="MasterTree"
                ItemTemplate="{StaticResource TaxonomyTemplate}"
                SelectedItemChanged="MasterTree_SelectedItemChanged"/>

            <Border BorderBrush="Gray" BorderThickness="1" Padding="8" Margin="8,0,0,0" Grid.Column="1">                
                <StackPanel x:Name="DetailsPanel" Margin="4">                    
                    <StackPanel Orientation="Horizontal">
                        <TextBlock Text="Classification: " FontWeight="Bold" Width="100" />
                        <TextBlock Text="{Binding Classification}" />
                    </StackPanel>
                    <StackPanel Orientation="Horizontal">
                        <TextBlock Text="Rank: " FontWeight="Bold" Width="100" />
                        <TextBlock Text="{Binding Rank}" />
                    </StackPanel>
                    <TextBlock Text="Information:" FontWeight="Bold" />                    
                    <TextBlock x:Name="LookupDetailText" TextWrapping="Wrap" />
                </StackPanel>
            </Border>
        </Grid>

        <!-- Source -->
        <src:SourceViewer xmlns:src="clr-namespace:System.Windows.Controls.Samples;assembly=System.Windows.Controls.Samples.Common" xmlns:sys="clr-namespace:System;assembly=mscorlib">
  <src:SourceFile Path="MasterDetailSample.xaml">
    <src:SourceFile.Source>
      <sys:String>&lt;!--
// (c) Copyright Microsoft Corporation.
// This source is subject to the Microsoft Public License (Ms-PL).
// Please see http://go.microsoft.com/fwlink/?LinkID=131993 for details.
// All other rights reserved.
--&gt;

&lt;UserControl
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:common="clr-namespace:System.Windows;assembly=System.Windows.Controls.Toolkit"
    xmlns:toolkit="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Toolkit"
    x:Class="System.Windows.Controls.Samples.MasterDetailSample"&gt;
    &lt;StackPanel&gt;

        &lt;StackPanel.Resources&gt;
            &lt;common:HierarchicalDataTemplate x:Key="TaxonomyTemplate" ItemsSource="{Binding Subclasses}"&gt;
                &lt;TextBlock Text="{Binding Classification}"/&gt;
            &lt;/common:HierarchicalDataTemplate&gt;
        &lt;/StackPanel.Resources&gt;

        &lt;ContentControl Content="Master/Detail" Style="{StaticResource Header}" /&gt;
        &lt;TextBlock Style="{StaticResource Information}"&gt;
                Demonstrates using a TreeView in a Master/Detail scenario.
                &lt;LineBreak/&gt;
                &lt;LineBreak/&gt;
                Two different methods of displaying detail data are 
                demonstrated.  The Classification and Rank TextBlocks are 
                bound directly to the SelectedItem in the TreeView everytime 
                the selection changes.  The larger information section 
                simulates a lookup into another data source using the 
                currently selected item as a key.
        &lt;/TextBlock&gt;
        
        &lt;Grid&gt;
            &lt;Grid.ColumnDefinitions&gt;
                &lt;ColumnDefinition Width="200" /&gt;
                &lt;ColumnDefinition Width="*" /&gt;
            &lt;/Grid.ColumnDefinitions&gt;
        
            &lt;toolkit:TreeView
                Grid.Column="0"
                x:Name="MasterTree"
                ItemTemplate="{StaticResource TaxonomyTemplate}"
                SelectedItemChanged="MasterTree_SelectedItemChanged"/&gt;

            &lt;Border BorderBrush="Gray" BorderThickness="1" Padding="8" Margin="8,0,0,0" Grid.Column="1"&gt;                
                &lt;StackPanel x:Name="DetailsPanel" Margin="4"&gt;                    
                    &lt;StackPanel Orientation="Horizontal"&gt;
                        &lt;TextBlock Text="Classification: " FontWeight="Bold" Width="100" /&gt;
                        &lt;TextBlock Text="{Binding Classification}" /&gt;
                    &lt;/StackPanel&gt;
                    &lt;StackPanel Orientation="Horizontal"&gt;
                        &lt;TextBlock Text="Rank: " FontWeight="Bold" Width="100" /&gt;
                        &lt;TextBlock Text="{Binding Rank}" /&gt;
                    &lt;/StackPanel&gt;
                    &lt;TextBlock Text="Information:" FontWeight="Bold" /&gt;                    
                    &lt;TextBlock x:Name="LookupDetailText" TextWrapping="Wrap" /&gt;
                &lt;/StackPanel&gt;
            &lt;/Border&gt;
        &lt;/Grid&gt;

        &lt;!-- Source --&gt;
    &lt;/StackPanel&gt;
&lt;/UserControl&gt;</sys:String>
    </src:SourceFile.Source>
  </src:SourceFile>
  <src:SourceFile Path="MasterDetailSample.xaml.cs">
    <src:SourceFile.Source>
      <sys:String>// (c) Copyright Microsoft Corporation.
// This source is subject to the Microsoft Public License (Ms-PL).
// Please see http://go.microsoft.com/fwlink/?LinkID=131993 for details.
// All other rights reserved.

using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.ComponentModel;

namespace System.Windows.Controls.Samples
{  
    /// &lt;summary&gt;
    /// Sample demonstrating the TreeView used in a Master/Detail scenario.
    /// &lt;/summary&gt;
    [Sample("MasterDetail", DifficultyLevel.Scenario)]
    [Category("TreeView")]   
    public partial class MasterDetailSample : UserControl
    {
        /// &lt;summary&gt;
        /// Initializes a new instance of the MasterDetailSample class.
        /// &lt;/summary&gt;
        public MasterDetailSample()
        {
            InitializeComponent();
            MasterTree.ItemsSource = Taxonomy.Life;
        }

        /// &lt;summary&gt;
        /// Handle the TreeView.SelectedItemChanged event.
        /// &lt;/summary&gt;
        /// &lt;param name="sender"&gt;The TreeView.&lt;/param&gt;
        /// &lt;param name="e"&gt;Event arguments.&lt;/param&gt;
        [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode", Justification = "The event handler is declared in XAML.")]
        private void MasterTree_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs&lt;object&gt; e)
        {
            // Setting the DataContext on the panel containing all the 
            // detail controls allows setting the Master object once
            // instead of once per control.
            DetailsPanel.DataContext = e.NewValue;

            // Simulate looking up data in another data source.
            if (e.NewValue != null)
            {
                Taxonomy taxonomy = (Taxonomy)e.NewValue;                
                
                StringBuilder information = new StringBuilder();
                information.AppendFormat(CultureInfo.CurrentCulture, "The {0} {1}, represents a signifigant portion of this sample text.\n\n", taxonomy.Rank, taxonomy.Classification);

                switch (taxonomy.Subclasses.Count)
                {
                    case 0:
                        information.Append("Doesn't contain any subclasses.");
                        break;                    

                    case 1:
                        information.Append("This contains only a single subclass.");
                        break;

                    default:
                        information.AppendFormat(CultureInfo.CurrentCulture, "Contains {0} subclasses.", taxonomy.Subclasses.Count);
                        break;
                }

                LookupDetailText.Text = information.ToString();
            }
        }
    }
}
</sys:String>
    </src:SourceFile.Source>
  </src:SourceFile>
  <src:SourceFile Path="MasterDetailSample.xaml.vb">
    <src:SourceFile.Source>
      <sys:String>' (c) Copyright Microsoft Corporation.
' This source is subject to the Microsoft Public License (Ms-PL).
' Please see http://go.microsoft.com/fwlink/?LinkID=131993 for details.
' All other rights reserved.

Imports Microsoft.VisualBasic
Imports System.Diagnostics.CodeAnalysis
Imports System.Globalization
Imports System.Text
Imports System.Windows
Imports System.Windows.Controls
Imports System.ComponentModel

''' &lt;summary&gt;
''' Sample demonstrating the TreeView used in a Master/Detail scenario.
''' &lt;/summary&gt;
&lt;Sample("MasterDetail", DifficultyLevel.Scenario), Category("TreeView")&gt; _
Partial Public Class MasterDetailSample
    Inherits UserControl
    ''' &lt;summary&gt;
    ''' Initializes a new instance of the MasterDetailSample class.
    ''' &lt;/summary&gt;
    &lt;System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors", Justification:="InitializeComponent Required by Visual Studio.")&gt; _
    Public Sub New()
        InitializeComponent()
        MasterTree.ItemsSource = Taxonomy.Life
    End Sub

    ''' &lt;summary&gt;
    ''' Handle the TreeView.SelectedItemChanged event.
    ''' &lt;/summary&gt;
    ''' &lt;param name="sender"&gt;The TreeView.&lt;/param&gt;
    ''' &lt;param name="e"&gt;Event arguments.&lt;/param&gt;
    &lt;SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode", Justification:="The event handler is declared in XAML.")&gt; _
    Private Sub MasterTree_SelectedItemChanged(ByVal sender As Object, ByVal e As RoutedPropertyChangedEventArgs(Of Object))
        ' Setting the DataContext on the panel containing all the 
        ' detail controls allows setting the Master object once
        ' instead of once per control.
        DetailsPanel.DataContext = e.NewValue

        ' Simulate looking up data in another data source.
        If e.NewValue IsNot Nothing Then
            Dim taxonomy As Taxonomy = CType(e.NewValue, Taxonomy)

            Dim information As New StringBuilder()
            information.AppendFormat(CultureInfo.CurrentCulture, "The {0} {1}, represents a signifigant portion of this sample text." &amp; Constants.vbLf + Constants.vbLf, taxonomy.Rank, taxonomy.Classification)

            Select Case taxonomy.Subclasses.Count
                Case 0
                    information.Append("Doesn't contain any subclasses.")

                Case 1
                    information.Append("This contains only a single subclass.")

                Case Else
                    information.AppendFormat(CultureInfo.CurrentCulture, "Contains {0} subclasses.", taxonomy.Subclasses.Count)
            End Select

            LookupDetailText.Text = information.ToString()
        End If
    End Sub
End Class

</sys:String>
    </src:SourceFile.Source>
  </src:SourceFile>
</src:SourceViewer>
    </StackPanel>
</UserControl>